package com.pai4j.search.messagequeue.consumer;
import com.pai4j.common.enums.messagequeue.MessageQueueEnum;
import com.pai4j.common.service.messagequeue.consumer.AbstractRedisQueueConsumer;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.dto.search.SyncSearchEngineListDTO;
import com.pai4j.search.service.voctor.ArticleMilvusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;


/**
 * 公众号推文consumer
 */
@Component
@Lazy(value = false)
@Slf4j
public class ArticleSyncRedisQueueConsumer extends AbstractRedisQueueConsumer<SyncSearchEngineListDTO> {


    @Autowired
    private ArticleMilvusService articleMilvusService;

    public void initBean() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                schedule();
            }
        }).start();
    }

    @Override
    protected SyncSearchEngineListDTO convert(String s) {

        return JsonUtil.fromJson(s, SyncSearchEngineListDTO.class);
    }

    @Override
    protected MessageQueueEnum getMessageQueue() {

        return MessageQueueEnum.QUEUE_SYNC_SEARCH_DB_MESSAGE;
    }

    @Override
    protected void receive(SyncSearchEngineListDTO data) {
        /**
         * 消息处理：向量库
         */
        articleMilvusService.doHandleSyncData(data);
    }

    @Override
    protected void error(SyncSearchEngineListDTO data, Exception ex) {

        /**
         * 消费失败，重新放回队列
         */
        backToQueue(data);
        /**
         * 预警通知
         */
        log.error("SyncDataRedisQueueConsumer error", ex);
    }
}
